﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using popLib;
using popLib.ExtensionClass;
using PhotoMgr.src.database;
using PhotoMgr.src.common;
using System.IO;
using System.Threading;

namespace PhotoMgr.src.gui.photo
{
    public partial class FinishRoundForm : Form
    {
        List<View_RoundImage> images;
        Round_NextRoundCapacity_Result nextRoundCapacity;
        int CurrentRoundID;
        int currentContestID;
        bool IsValidated = false;
        double compareMark = -1;
        double compareMarkSub = -1;

        public FinishRoundForm(int contestID, int roundID)
        {
            InitializeComponent();

            this.Icon = PhotoMgr.Properties.Resources.logo32;

            currentContestID = contestID;
            CurrentRoundID = roundID;
            nextRoundCapacity = DBManager.Round_NextRoundCapacity(roundID);

            dgv_image.AutoGenerateColumns = false;
            Global.InitDataGridView(dgv_image, new DataGridViewColumn[] { 
                Global.GenColumn("#", "IsScore", Global.GenColumnType.CheckBox),
                Global.GenColumn("Hình ảnh", "ImageTitle").SetSizeMode(DataGridViewAutoSizeColumnMode.Fill),
                Global.GenColumn("Điểm", "Score"),
                Global.GenColumn("Điểm phụ", "SubScore"),                
                Global.GenColumn(" ", "RoundImageID").SetVisible(false),
            });

        }

        void ReloadData()
        {
            
            images = DBManager.Round_ListImage(CurrentRoundID, ScoreType.MainScore);
            images = images.OrderByDescending(x => x.ThirdScore).OrderByDescending(x => x.SubScore).OrderByDescending(x => x.Score).ToList();

            dgv_image.DataSource = null;
            dgv_image.DataSource = images;

            if (nextRoundCapacity.NextLevelCapacity == -1 && nextRoundCapacity.NextLevel == -1)
            {
                btn_remark.Enabled = false;
                FormLib.alert("Vòng thi cuối cùng của cuộc thi ảnh đã kết thúc");
                return;
            }

            compareMarkSub = compareMark = -1;
            IsValidated = true;
            double nextMark = -1, nextMarkSub = -1;

            if (nextRoundCapacity.NextLevelCapacity - 1 < images.Count)
            {
                compareMark = (double)images[nextRoundCapacity.NextLevelCapacity - 1].Score;
                compareMarkSub = (double)images[nextRoundCapacity.NextLevelCapacity - 1].SubScore;

                if (nextRoundCapacity.NextLevelCapacity < images.Count)
                {
                    nextMark = (double)images[nextRoundCapacity.NextLevelCapacity].Score;
                    nextMarkSub = (double)images[nextRoundCapacity.NextLevelCapacity].SubScore;                    
                    IsValidated = compareMark != nextMark || compareMarkSub != nextMarkSub;
                }
            }            

            for (int i = 0; i < dgv_image.Rows.Count; i++)
            {
                if (IsValidated)
                {
                    if (i < nextRoundCapacity.NextLevelCapacity)
                    {
                        dgv_image.Rows[i].DefaultCellStyle.BackColor = Color.Blue;
                    }
                    else
                    {
                        dgv_image.Rows[i].DefaultCellStyle.BackColor = Color.Empty;
                    }
                }
                else
                {
                    if (images[i].Score == compareMark && images[i].SubScore == compareMarkSub)
                    {
                        dgv_image.Rows[i].DefaultCellStyle.BackColor = Color.Red;
                    }
                    else
                    {
                        dgv_image.Rows[i].DefaultCellStyle.BackColor = Color.Empty;
                    }
                }
            }                        

            btn_OK.Enabled = IsValidated;
            btn_remark.Enabled = !IsValidated;
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            DBManager.Contest_FinishRound(CurrentRoundID);
            FormLib.alert("Vòng thi đã hoàn tất.");
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void btn_remark_Click(object sender, EventArgs e)
        {
            DBManager.Round_UpdateForSubScore(CurrentRoundID, compareMark, compareMarkSub);
            new HostForm2(currentContestID, ScoreType.SubScore).ShowDialog();
            ReloadData();
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FinishRoundForm_Load(object sender, EventArgs e)
        {
            ReloadData();
        }

        private void dgv_image_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            View_RoundImage img = images[e.RowIndex];

            pbx_image.Image = System.Drawing.Image.FromFile(img.ImagePath);
            pbx_image.SizeMode = PictureBoxSizeMode.Zoom;
            lbl_imageTitle.Text = img.ImageTitle;
            lbl_artistName.Text = "Tác giả: " + img.ArtistName;
            lbl_note.Text = "";

            GC.Collect();
        }
    }
}
